#!/usr/bin/env/python3.7
# -*- coding: utf-8 -*-

"""
@File    : upload_sensor.py
@Time    : 12/12/2021
@Author  : Silence
@Version : 1.0
@Contact : silence2021silence@163.com
@License : (C)Copyright 2021-2021, Silence
@Desc    : None
"""

import pymysql
import read_arduino

database = pymysql.connect(host="120.24.51.156", user="dormitory_system", password="123456",
                                   database="dormitory_system", charset="utf8")

class UploadSensor():
    def upload_sensor():
        cursor = database.cursor()
        sql_1 = """
                INSERT INTO `dormitory_system`.`original_data` (
	                `id`, `student_number`, `name`, `room`,
	                `grade`, `class`, `ip_address`, `upload_datetime`,
	                `infrared_value`, `temperature_value`,
	                `weight_value`, `volume_value`,
	                `smoke_ttl`, `smoke_value`
                )
                VALUES
	                (
		                NULL, '10001', '张一', '101', '高三',
		                '1班', '192.168.8.102', '2021-11-16 22:40:00',
		                '%s', '%s', '%s', '%s', '%s', '%s'
	                )
                        """
        (infrared_value, temperature_value, weight_value, volume_value,
         smoke_ttl, smoke_value) = read_arduino.ReadArduino.read_arduino()
        sql_2 = sql_1 % (infrared_value, temperature_value, weight_value, volume_value,
                         smoke_ttl, smoke_value)
        database.ping(reconnect=True)
        cursor.execute(sql_2)
        database.commit()
        cursor.close()
        database.close()